package whf.framework.web.tag;

import java.lang.reflect.Constructor;
import java.util.Map;

import whf.framework.datasource.DataRow;
import whf.framework.exception.PropertyException;
import whf.framework.i18n.ApplicationResource;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.PermissionUtils;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 *
 */
@SuppressWarnings("unchecked")
public class TableRow {
	private static Log log = LogFactory.getLog(TableRow.class);
	
	private final static String NOPERM_MSG = ApplicationResource.get("auth.no_permission.msg");

	private int number;

	private DataRow rowObject;
	
	/**
	 * 粉状类
	 * @property Class:wrapClass
	 */
	private Class wrapClass;
	
	/**
	 * 对象属性的值有两种：
	 * 1、手动赋值，往往出现在TableColumnTag中赋值的情况
	 * 2、从rowObject中获取属性
	 * 其中第一种有限极高，如果存在attributeValues的数字，优先作为返回值
	 * @property Map<String,Object>:attributeValues
	 */
	private Map<String, Object> attributeValues = Utils.newHashMap();
	
	/**
	 * 用来设置基本数据之外的一些数据
	 * @property String:rowExt
	 */
	private String rowExt;

	public TableRow(DataRow rowObj, int rowNumber, Class wrapClass) {
		this.number = rowNumber;
		this.wrapClass = wrapClass;
		if(wrapClass != null){
			this.rowObject = this.getWrapObject(rowObj);
		} else {
			this.rowObject = rowObj;
		}
	}

	public int getNumber() {
		return number;
	}

	/**
	 * @return Returns the rowExt.
	 */
	public final String getRowExt() {
		return rowExt;
	}

	/**
	 * @modify wanghaifeng Sep 5, 2006 10:32:25 PM
	 * @param rowExt
	 */
	public void setRowExt(String rowExt){
		this.rowExt = rowExt;
	}
	
	public DataRow getRowObject() {
		return rowObject;
	}
	
	public void setPropertyValue(String property, Object value){
		this.attributeValues.put(property, value);
	}
	
	/**
	 * 使用封装类创建新对象
	 * @modify wanghaifeng Aug 22, 2006 10:01:15 PM
	 * @param bo
	 * @return
	 */
	private DataRow getWrapObject(DataRow bo){
		if(this.wrapClass == null){
			return bo;
		} else {
			DataRow result = null;
			try{
				Constructor[] cs = wrapClass.getConstructors();
				for(int i=0; cs != null && i < cs.length; i++){
					Class[] parameterTypes = cs[i].getParameterTypes();
					if(parameterTypes != null && parameterTypes.length == 1 && 
							parameterTypes[0] == bo.getClass()){
						result = (DataRow)cs[i].newInstance(new Object[]{bo});
					}
				}
			}catch(Exception e){
				log.error(this, e);
			}
			return result;
		}
	}
	
	/**
	 * 获取属性值
	 * @modify wanghaifeng Jan 26, 2007 9:19:12 AM
	 * @param property 属性名称
	 * @return
	 */
	public Object getPropertyValue(String property){
		Object result = null;
		if(attributeValues.keySet().contains(property)){
			result = attributeValues.get(property);
		} else {
			try{
				if(PermissionUtils.hasPermission(this.rowObject)){
					result = this.rowObject.getProperty(property);
				} else {
					result = NOPERM_MSG;
				}
			}catch(PropertyException ignoreException){
				//
			}
		}
		return result;
	}
}
